//
//  StwxDetailItemViewModel.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/10/24.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "StwxDetailItemViewModel.h"

#import "ArticleInfoData.h"

@interface StwxDetailItemViewModel ()

/// 商品模型
//@property (nonatomic, readwrite, strong) EnterpriseInfoData* epInfoData;

@property (nonatomic, readwrite, copy) NSString* top;
@property (nonatomic, readwrite, copy) NSString* title;
@property (nonatomic, readwrite, copy) NSString* name;
@property (nonatomic, readwrite, copy) NSString* sth;
@property (nonatomic, readwrite, copy) NSString* source;
@property (nonatomic, readwrite, copy) NSString* examine;
@property (nonatomic, readwrite, copy) NSString* content;
@property (nonatomic, readwrite, copy) NSString* imgurl;
@property (nonatomic, readwrite, copy) NSString* commentNum;
@property (nonatomic, readwrite, copy) NSString* goodNum;
@property (nonatomic, readwrite, copy) NSString* downloadFileName;
@property (nonatomic, readwrite, copy) NSString* downloadFilePath;

@property (nonatomic, readwrite, copy) NSString* articleID;
@property (nonatomic, readwrite, assign) NSInteger type;

@end

@implementation StwxDetailItemViewModel

- (instancetype)initWithData:(id)data cellHeight:(CGFloat)cellHeight
{
    self = [super init];
    if (self)
    {
        //        _epInfoData = data;
        
        _isHaveCtrl = NO;
        
        _tabIndex = 0;
    
        _data = data;
        
        /// cellHeight+2（2容错）
        _cellHeight = MHUIScaleValue(cellHeight)+2;
        
        @weakify(self);
        self.goUserSpaceCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
            
            return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
                
                [subscriber sendNext:nil];
                [subscriber sendCompleted];
                
                return nil;
            }];
        }];
        
        [[[RACObserve(self, data) takeUntil:[self rac_willDeallocSignal]] distinctUntilChanged] subscribeNext:^(id x)
        {
            @strongify(self);
            
            ArticleInfoData* infoData = x;
            
            self.top = @"携手生态云";
            self.title = infoData.title;
            self.name = infoData.user.nickname;
            self.sth = HCStrFormat(@"生态号 | %@", infoData.addTime);
            self.source = HCStrFormat(@"文章来源：%@", infoData.from);
            NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithData:[infoData.content dataUsingEncoding: NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType } documentAttributes:nil error:nil];
            self.content = attrStr.string;
            self.imgurl = infoData.user.headimg;
            self.commentNum = HCStrFormat(@" 评论 %@ ", infoData.commentCount);
            self.goodNum = HCStrFormat(@" 点赞 %@ ", infoData.praiseCount);
            
            self.downloadFileName = ([infoData.type isEqualToString:@"2"]&&infoData.download&&[infoData.download isNotBlank]) ? [infoData.download componentsSeparatedByString:@"/"].lastObject : @"";
            if ([self.downloadFileName isNotBlank])
            {
                self.downloadFileName = HCStrFormat(@"《%@》点击查看文档", self.downloadFileName);
            }
            self.downloadFilePath = infoData.download;
            
            self.type = [infoData.type integerValue];
            self.articleID = infoData.articleID;
        }];
    }
    
    return self;
}

@end
